import { lstat } from 'node:fs/promises'
import { cwd } from 'node:process'
import { ipcRenderer } from 'electron'
import BetterSqlite3 from 'better-sqlite3'
import path from 'node:path'
import fs from 'fs-extra'

ipcRenderer.on('main-process-message', (_event, dataPath) => {
  let dbPath = path.join(dataPath, "electron-vue-vite")
  if (!fs.existsSync(dbPath)) {
    fs.mkdirSync(dbPath, {
      recursive: true,
    })
  }

  // 数据库文件路径
  dbPath = path.join(dbPath, `writer_database.db`)

  // 初始化数据库
  const db = BetterSqlite3(dbPath, {
    verbose: console.log,
  })

  // 设置数据库连接的 PRAGMA
  db.pragma('journal_mode = WAL')
  db.pragma('cache_size = 32000')
  console.log('创建数据库连接')
  console.log('dbPath:', dbPath)
})

lstat(cwd()).then(stats => {
  console.log('[fs.lstat]', stats)
}).catch(err => {
  console.error(err)
})
